﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:configinfo
	/// </summary>
	public partial class configinfo
	{
		public configinfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("cid", "configinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int cid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from configinfo");
			strSql.Append(" where cid=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4)
			};
			parameters[0].Value = cid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(lcs.Model.configinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into configinfo(");
			strSql.Append("tsgj,tsps,ygyqs,ygyjs,psyqs,psyjs,gybz,psbz,zlbz)");
			strSql.Append(" values (");
			strSql.Append("@tsgj,@tsps,@ygyqs,@ygyjs,@psyqs,@psyjs,@gybz,@psbz,@zlbz)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@tsgj", SqlDbType.Int,4),
					new SqlParameter("@tsps", SqlDbType.Int,4),
					new SqlParameter("@ygyqs", SqlDbType.Int,4),
					new SqlParameter("@ygyjs", SqlDbType.Int,4),
					new SqlParameter("@psyqs", SqlDbType.Int,4),
					new SqlParameter("@psyjs", SqlDbType.Int,4),
					new SqlParameter("@gybz", SqlDbType.Int,4),
					new SqlParameter("@psbz", SqlDbType.Int,4),
					new SqlParameter("@zlbz", SqlDbType.Int,4)};
			parameters[0].Value = model.tsgj;
			parameters[1].Value = model.tsps;
			parameters[2].Value = model.ygyqs;
			parameters[3].Value = model.ygyjs;
			parameters[4].Value = model.psyqs;
			parameters[5].Value = model.psyjs;
			parameters[6].Value = model.gybz;
			parameters[7].Value = model.psbz;
			parameters[8].Value = model.zlbz;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcs.Model.configinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update configinfo set ");
			strSql.Append("tsgj=@tsgj,");
			strSql.Append("tsps=@tsps,");
			strSql.Append("ygyqs=@ygyqs,");
			strSql.Append("ygyjs=@ygyjs,");
			strSql.Append("psyqs=@psyqs,");
			strSql.Append("psyjs=@psyjs,");
			strSql.Append("gybz=@gybz,");
			strSql.Append("psbz=@psbz,");
			strSql.Append("zlbz=@zlbz");
			strSql.Append(" where cid=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@tsgj", SqlDbType.Int,4),
					new SqlParameter("@tsps", SqlDbType.Int,4),
					new SqlParameter("@ygyqs", SqlDbType.Int,4),
					new SqlParameter("@ygyjs", SqlDbType.Int,4),
					new SqlParameter("@psyqs", SqlDbType.Int,4),
					new SqlParameter("@psyjs", SqlDbType.Int,4),
					new SqlParameter("@gybz", SqlDbType.Int,4),
					new SqlParameter("@psbz", SqlDbType.Int,4),
					new SqlParameter("@zlbz", SqlDbType.Int,4),
					new SqlParameter("@cid", SqlDbType.Int,4)};
			parameters[0].Value = model.tsgj;
			parameters[1].Value = model.tsps;
			parameters[2].Value = model.ygyqs;
			parameters[3].Value = model.ygyjs;
			parameters[4].Value = model.psyqs;
			parameters[5].Value = model.psyjs;
			parameters[6].Value = model.gybz;
			parameters[7].Value = model.psbz;
			parameters[8].Value = model.zlbz;
			parameters[9].Value = model.cid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int cid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from configinfo ");
			strSql.Append(" where cid=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4)
			};
			parameters[0].Value = cid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string cidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from configinfo ");
			strSql.Append(" where cid in ("+cidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.configinfo GetModel(int cid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 cid,tsgj,tsps,ygyqs,ygyjs,psyqs,psyjs,gybz,psbz,zlbz from configinfo ");
			strSql.Append(" where cid=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4)
			};
			parameters[0].Value = cid;

			lcs.Model.configinfo model=new lcs.Model.configinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}
        public lcs.Model.configinfo GetModel()
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 cid,tsgj,tsps,ygyqs,ygyjs,psyqs,psyjs,gybz,psbz,zlbz from configinfo ");           
            lcs.Model.configinfo model = new lcs.Model.configinfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.configinfo DataRowToModel(DataRow row)
		{
			lcs.Model.configinfo model=new lcs.Model.configinfo();
			if (row != null)
			{
				if(row["cid"]!=null && row["cid"].ToString()!="")
				{
					model.cid=int.Parse(row["cid"].ToString());
				}
				if(row["tsgj"]!=null && row["tsgj"].ToString()!="")
				{
					model.tsgj=int.Parse(row["tsgj"].ToString());
				}
				if(row["tsps"]!=null && row["tsps"].ToString()!="")
				{
					model.tsps=int.Parse(row["tsps"].ToString());
				}
				if(row["ygyqs"]!=null && row["ygyqs"].ToString()!="")
				{
					model.ygyqs=int.Parse(row["ygyqs"].ToString());
				}
				if(row["ygyjs"]!=null && row["ygyjs"].ToString()!="")
				{
					model.ygyjs=int.Parse(row["ygyjs"].ToString());
				}
				if(row["psyqs"]!=null && row["psyqs"].ToString()!="")
				{
					model.psyqs=int.Parse(row["psyqs"].ToString());
				}
				if(row["psyjs"]!=null && row["psyjs"].ToString()!="")
				{
					model.psyjs=int.Parse(row["psyjs"].ToString());
				}
				if(row["gybz"]!=null && row["gybz"].ToString()!="")
				{
					model.gybz=int.Parse(row["gybz"].ToString());
				}
				if(row["psbz"]!=null && row["psbz"].ToString()!="")
				{
					model.psbz=int.Parse(row["psbz"].ToString());
				}
				if(row["zlbz"]!=null && row["zlbz"].ToString()!="")
				{
					model.zlbz=int.Parse(row["zlbz"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select cid,tsgj,tsps,ygyqs,ygyjs,psyqs,psyjs,gybz,psbz,zlbz ");
			strSql.Append(" FROM configinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" cid,tsgj,tsps,ygyqs,ygyjs,psyqs,psyjs,gybz,psbz,zlbz ");
			strSql.Append(" FROM configinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM configinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.cid desc");
			}
			strSql.Append(")AS Row, T.*  from configinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "configinfo";
			parameters[1].Value = "cid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

